package com.seth.myapplication.surface;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.seth.myapplication.LoginActivity;
import com.seth.myapplication.R;
import com.seth.myapplication.UsrInfoActivity;
import com.seth.myapplication.bean.Rewords;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.service.NetWorkService;

/**
 * Created by Seth on 2017/11/14.
 */

public class UserInfomation extends Fragment {

    private static final String EXTRA_USER_POSITION = "11111111";
    private User mUser;
    private EditText txtNickname;
    private EditText txtRealname;
    private EditText txtStdNum;
    private EditText txtUsrEmail;
    private EditText txtUsrDetail;
    private ImageView backUser;
    private Button infoSave;
    private RadioGroup usrSex;

    private boolean isChecked = false;
    private int userSex;
    private boolean updateNameFlag = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_usr_infomation, container, false);
        Bundle bundle = getArguments();
        mUser = (User) bundle.getSerializable(EXTRA_USER_POSITION);
        initView(view);
        initEvent();

        return view;
    }

    private void initView(View view) {
        txtNickname = (EditText) view.findViewById(R.id.usr_nickname);
        txtRealname = (EditText) view.findViewById(R.id.usr_realname);
        txtStdNum = (EditText) view.findViewById(R.id.usr_std);
        txtUsrEmail = (EditText) view.findViewById(R.id.usr_email);
        txtUsrDetail = (EditText) view.findViewById(R.id.details_txt);
        backUser = (ImageView) view.findViewById(R.id.back_usr);
        infoSave = (Button) view.findViewById(R.id.info_save);
        usrSex = (RadioGroup) view.findViewById(R.id.sex_group);

    }

    private void initEvent() {
        txtRealname.setText(mUser.getRealname());
        txtStdNum.setText(mUser.getStdNum());
        txtUsrEmail.setText(mUser.getEmail());
        txtUsrDetail.setText(mUser.getDetail());
        txtNickname.setText(mUser.getNickname());

        usrSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.sex_boy:
                        isChecked = true;
                        userSex = 0;
                        break;
                    case R.id.sex_girl:
                        isChecked = true;
                        userSex = 1;
                        break;
                    default:
                        isChecked = false;
                }
            }
        });
        infoSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String realname = txtRealname.getText().toString();
                String stdNum = txtStdNum.getText().toString();
                String email = txtUsrEmail.getText().toString();
                String details = txtUsrDetail.getText().toString();
                String nickname = txtNickname.getText().toString();

                if(!isChecked &&nickname.equals(mUser.getNickname())&& realname.equals(mUser.getRealname()) && stdNum.equals(mUser.getStdNum()) && email.equals(mUser.getEmail()) && details.equals(mUser.getDetail())){
                    Toast.makeText(getContext(), "未作任何改变", Toast.LENGTH_SHORT).show();
                }else{
                    if(isChecked){
                        mUser.setSex(userSex);
                    }
                    if(!nickname.isEmpty()&& !nickname.equals(mUser.getNickname())){
                        mUser.setNickname(nickname);
                    }
                    if(!realname.isEmpty() && !realname.equals(mUser.getRealname())){
                        Log.i("updateInfo",realname);
                        mUser.setRealname(realname);
                        updateNameFlag = true;
                    }
                    if(!stdNum.isEmpty() && !stdNum.equals(mUser.getStdNum())){
                        mUser.setStdNum(stdNum);
                        Log.i("updateInfo",stdNum);
                    }
                    if(!email.isEmpty() && !email.equals(mUser.getEmail())){
                        mUser.setEmail(email);
                        Log.i("updateInfo",email);
                    }
                    if(!details.equals(mUser.getDetail())){
                        mUser.setDetail(details);
                        Log.i("updateInfo",details);
                    }
                    new NetWorkService(getActivity()).updateUsr(mUser);
                }
            }
        });

        backUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用返回间
                getActivity().onBackPressed();
            }
        });
    }

    @Override
    public void onDestroy() {
        if (updateNameFlag){
            Log.i("updateInfo","setResult");
            updateNameFlag = false;
            Intent backIntent = new Intent();
            backIntent.putExtra("user",mUser);
            getActivity().setResult(4,backIntent);
        }
        super.onDestroy();
    }

    //    @Override
//    public void onCheckedChanged(RadioGroup group, int checkedId) {
//        switch (checkedId){
//            case R.id.sex_boy:
//                isChecked = true;
//                userSex = 0;
//                break;
//            case R.id.sex_girl:
//                isChecked = true;
//                userSex = 1;
//                break;
//            default:
//                isChecked = false;
//        }
//    }

}
